﻿using ChartManager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ChartManager.CustomControls
{
    /// <summary>
    /// 带歌曲信息的输入框
    /// </summary>
    public class SongTextBox : CellTextBox
    {
        public delegate void SongEventHandler(object sender, SongEventArgs e);
        public event SongEventHandler SongChanged;
        /// <summary>
        /// SongChanged事件处理
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnSongChanged(SongEventArgs e)
        {
            // 修改输入框文本为空或新值文本
            Text = e.newSong != null ? $"{e.newSong.SingersAsString} - {e.newSong.Name}" : string.Empty;
            // 执行自定义事件处理
            SongChanged?.Invoke(this, e);
        }
        public Song song
        {
            get { return _song; }
            set
            {
                // 新值与旧值相等时，返回不操作
                if (_song == null && value == null) return;
                if (_song != null && _song.Equals(value)) return;
                // 产生新值与旧值事件参数
                var args = new SongEventArgs(_song, value);
                // 填入新值
                _song = value;
                // 触发SongChanged事件
                OnSongChanged(args);
                // 禁用输入框
                ReadOnly = true;
            }
        }
        private Song _song;
    }

    public class SongEventArgs : EventArgs
    {
        public SongEventArgs(Song oldSong, Song newSong)
        {
            this.oldSong = oldSong;
            this.newSong = newSong;
        }
        /// <summary>
        /// 旧值
        /// </summary>
        public Song oldSong { get; }
        /// <summary>
        /// 新值
        /// </summary>
        public Song newSong { get; }
    }
}